﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GtsServer.Model;
using System.Data.SqlClient;
using System.Data;
using GtsServer.Common;

namespace GtsServer.DAL
{
    public class NewDeliveryDAL
    {
        public NewDeliveryDAL()
		{}
		#region  Method

		/// <summary>
		/// 增加一条数据
		/// </summary>
        public int Add(GtsServer.Model.NewDeliveryMould model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into NewDelivery(");
            strSql.Append("CommunicationID,NewsType,NewsTitle,NewsContent,Priority,StartTime,EndTime,CreateTime,State,DeliveryNum,DeliveryType)");
			strSql.Append(" values (");
            strSql.Append("@CommunicationID,@NewsType,@NewsTitle,@NewsContent,@Priority,@StartTime,@EndTime,@CreateTime,@State,@DeliveryNum,@DeliveryType)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@CommunicationID", SqlDbType.Int,4),
					new SqlParameter("@NewsType", SqlDbType.VarChar,50),
					new SqlParameter("@NewsTitle", SqlDbType.VarChar,50),
					new SqlParameter("@NewsContent", SqlDbType.VarChar,2000),
					new SqlParameter("@Priority", SqlDbType.Int,4),
					new SqlParameter("@StartTime", SqlDbType.DateTime),
					new SqlParameter("@EndTime", SqlDbType.DateTime),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@State", SqlDbType.Int,4),
					new SqlParameter("@DeliveryNum", SqlDbType.Int,4),
                    new SqlParameter("@DeliveryType", SqlDbType.Int,4)};
			parameters[0].Value = model.CommunicationID;
			parameters[1].Value = model.NewsType;
			parameters[2].Value = model.NewsTitle;
			parameters[3].Value = model.NewsContent;
			parameters[4].Value = model.Priority;
			parameters[5].Value = model.StartTime;
			parameters[6].Value = model.EndTime;
			parameters[7].Value = model.CreateTime;
			parameters[8].Value = model.State;
			parameters[9].Value = model.DeliveryNum;
            parameters[10].Value = model.DeliveryType;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
        public bool Update(GtsServer.Model.NewDeliveryMould model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update NewDelivery set ");
			strSql.Append("CommunicationID=@CommunicationID,");
			strSql.Append("NewsType=@NewsType,");
			strSql.Append("NewsTitle=@NewsTitle,");
			strSql.Append("NewsContent=@NewsContent,");
			strSql.Append("Priority=@Priority,");
			strSql.Append("StartTime=@StartTime,");
			strSql.Append("EndTime=@EndTime,");
			strSql.Append("CreateTime=@CreateTime,");
			strSql.Append("State=@State,");
			strSql.Append("DeliveryNum=@DeliveryNum");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@CommunicationID", SqlDbType.Int,4),
					new SqlParameter("@NewsType", SqlDbType.VarChar,50),
					new SqlParameter("@NewsTitle", SqlDbType.VarChar,50),
					new SqlParameter("@NewsContent", SqlDbType.VarChar,2000),
					new SqlParameter("@Priority", SqlDbType.Int,4),
					new SqlParameter("@StartTime", SqlDbType.DateTime),
					new SqlParameter("@EndTime", SqlDbType.DateTime),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@State", SqlDbType.Int,4),
					new SqlParameter("@DeliveryNum", SqlDbType.Int,4),
					new SqlParameter("@ID", SqlDbType.Int,4)};
			parameters[0].Value = model.CommunicationID;
			parameters[1].Value = model.NewsType;
			parameters[2].Value = model.NewsTitle;
			parameters[3].Value = model.NewsContent;
			parameters[4].Value = model.Priority;
			parameters[5].Value = model.StartTime;
			parameters[6].Value = model.EndTime;
			parameters[7].Value = model.CreateTime;
			parameters[8].Value = model.State;
			parameters[9].Value = model.DeliveryNum;
			parameters[10].Value = model.ID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from NewDelivery ");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
};
			parameters[0].Value = ID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string IDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from NewDelivery ");
			strSql.Append(" where ID in ("+IDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
        public GtsServer.Model.NewDeliveryMould GetModel(int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 ID,CommunicationID,NewsType,NewsTitle,NewsContent,Priority,StartTime,EndTime,CreateTime,State,DeliveryNum from NewDelivery ");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
};
			parameters[0].Value = ID;

            GtsServer.Model.NewDeliveryMould model = new NewDeliveryMould();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["ID"]!=null && ds.Tables[0].Rows[0]["ID"].ToString()!="")
				{
					model.ID=int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["CommunicationID"]!=null && ds.Tables[0].Rows[0]["CommunicationID"].ToString()!="")
				{
					model.CommunicationID=int.Parse(ds.Tables[0].Rows[0]["CommunicationID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["NewsType"]!=null && ds.Tables[0].Rows[0]["NewsType"].ToString()!="")
				{
					model.NewsType=ds.Tables[0].Rows[0]["NewsType"].ToString();
				}
				if(ds.Tables[0].Rows[0]["NewsTitle"]!=null && ds.Tables[0].Rows[0]["NewsTitle"].ToString()!="")
				{
					model.NewsTitle=ds.Tables[0].Rows[0]["NewsTitle"].ToString();
				}
				if(ds.Tables[0].Rows[0]["NewsContent"]!=null && ds.Tables[0].Rows[0]["NewsContent"].ToString()!="")
				{
					model.NewsContent=ds.Tables[0].Rows[0]["NewsContent"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Priority"]!=null && ds.Tables[0].Rows[0]["Priority"].ToString()!="")
				{
					model.Priority=int.Parse(ds.Tables[0].Rows[0]["Priority"].ToString());
				}
				if(ds.Tables[0].Rows[0]["StartTime"]!=null && ds.Tables[0].Rows[0]["StartTime"].ToString()!="")
				{
					model.StartTime=DateTime.Parse(ds.Tables[0].Rows[0]["StartTime"].ToString());
				}
				if(ds.Tables[0].Rows[0]["EndTime"]!=null && ds.Tables[0].Rows[0]["EndTime"].ToString()!="")
				{
					model.EndTime=DateTime.Parse(ds.Tables[0].Rows[0]["EndTime"].ToString());
				}
				if(ds.Tables[0].Rows[0]["CreateTime"]!=null && ds.Tables[0].Rows[0]["CreateTime"].ToString()!="")
				{
					model.CreateTime=DateTime.Parse(ds.Tables[0].Rows[0]["CreateTime"].ToString());
				}
				if(ds.Tables[0].Rows[0]["State"]!=null && ds.Tables[0].Rows[0]["State"].ToString()!="")
				{
					model.State=int.Parse(ds.Tables[0].Rows[0]["State"].ToString());
				}
				if(ds.Tables[0].Rows[0]["DeliveryNum"]!=null && ds.Tables[0].Rows[0]["DeliveryNum"].ToString()!="")
				{
					model.DeliveryNum=int.Parse(ds.Tables[0].Rows[0]["DeliveryNum"].ToString());
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
        public DataSet GetList(int pageIndex, int pageSize, string strWhere = "")
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select * from ( select n.ID,c.name as CommunicationName,n.CommunicationID,n.NewsType,n.NewsTitle,n.NewsContent,n.Priority,n.StartTime,n.EndTime,n.CreateTime,n.State,case n.State when '1' then '启用' when '0' then '关闭' end as StateName,n.DeliveryNum,ROW_NUMBER() OVER(Order by n.CreateTime desc) as RowNumber ");
            strSql.Append(" FROM NewDelivery n left join Communication c on n.CommunicationID=c.ID");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
            strSql.Append(" ) a where  RowNumber BETWEEN " + ((pageIndex - 1) * pageSize + 1) + " and " + pageIndex * pageSize + ";");
            strSql.Append(" select COUNT(1) from NewDelivery n left join Communication c on n.CommunicationID=c.ID");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
			return DbHelperSQL.Query(strSql.ToString());
		}
        public DataSet GetListByID(int ID = 0)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select NewsTitle,NewsContent,DeliveryType");
            strSql.Append(" FROM NewDelivery");
            if (ID>0)
            {
                strSql.Append(" where ID="+ID );
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "NewDelivery";
			parameters[1].Value = "ID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

